package com.hxm.messenger;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.annotation.Nullable;

/**
 * Created by hxm on 2017/2/9.
 */

public class Server extends Service {

    public static final int SUM = 1000;
    Messenger messenger = new Messenger(new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Message msgToClient = Message.obtain();
            switch (msg.what) {
                case SUM:
                    msgToClient.what = 2000;
                    msgToClient.arg1 = msg.arg1 + msg.arg2;
                    try {
                        msg.replyTo.send(msgToClient);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    });

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return messenger.getBinder();
    }
}
